package com.lris.handle;

import cn.hutool.core.date.DateUtil;
import com.lris.pojodata.Command;
import com.lris.pojodata.Message;
import com.lris.rest.Result;
import com.lris.server.ImServer;
import io.netty.channel.ChannelHandlerContext;

public class ConnectionHandle {
    public static void execute(ChannelHandlerContext ctx, Command command){
//        if (ImServer.USERS.containsKey(command.getNickname())){
//            ctx.writeAndFlush(Result.fail(command.getNickname()+"已存在"));
//            ctx.channel().deregister();
//            return;
//        }
        ImServer.USERS.put(command.getUserid().toString(),ctx.channel());
        System.out.println("id为:"+command.getUserid()+"的"+command.getNickname()+"与服务器成功建立连接");
        Message message = new Message();
        message.setAddTime(DateUtil.date().toLocalDateTime());
        message.setAddNickname("加入了服务器");
        message.setMsgType(10000);
//        ctx.writeAndFlush(Result.success(message));
        ctx.channel().writeAndFlush(Result.success(ImServer.USERS.keySet()));
    }
}
